package com.dataseek.iot.mqtt.callback;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.*;
import org.springframework.stereotype.Component;

/**
 * @author cc
 * @Title: MQTT 客户端Callback
 * @Description: MQTT 客户端Callback
 * @date 2021/8/272:21 下午
 * @since 1.0
 */
@Slf4j
@Component
public class DefaultCallback implements MqttCallbackExtended {



    /**
     * 断开连接执行方法
     * @param throwable
     */
    @Override
    public void connectionLost(Throwable throwable) {
        //TODO 断开执行
        log.error(throwable.getMessage(), throwable);
    }

    /**
     * 订阅消息处理
     * @param s
     * @param mqttMessage
     */
    @Override
    public void messageArrived(String s, MqttMessage mqttMessage) {
        log.info("监听到数据");
        log.info("[{}] : {}", s, new String(mqttMessage.getPayload()));
    }


    /**
     * publish 发送成功执行
     * @param iMqttDeliveryToken
     */
    @SneakyThrows
    @Override
    public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
        MqttException exception = iMqttDeliveryToken.getException();
        if(exception==null){
            log.info("数据发送成功");
        }else {
            log.error(exception.getMessage());
        }
    }

    /**
     * 连接成功后调用
     * @param b
     * @param s
     */
    @Override
    public void connectComplete(boolean b, String s) {
        //TODO 连接成功后调用，从缓存中将topics重新订阅
        //TODO client.subscribe(topics,Qos);
        log.info("进入回调, 相关参数值为 {}, {}", b, s);
    }


}
